import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;


/*
 * @lc app=leetcode.cn id=71 lang=java
 *
 * [71] 简化路径
 */

// @lc code=start
class Solution {
    public String simplifyPath(String path) {
        String[] strs = path.split("/");
        String resultPath = "/";
        Stack<String> s1 = new Stack<String>();
        Stack<String> s2 = new Stack<>();
        for ( String str: strs){
            System.out.println(str);
            if ( str.equals("..") && !(s1.isEmpty())){
                s1.pop();
            }else if (!(str.equals(".")) && !(str.equals("")) && !(str.equals(".."))){
                s1.push(str);
            }
        } 
        while ( !(s1.isEmpty()) ){
            s2.push(s1.pop());
        }
        while ( !(s2.isEmpty())){
            if (s2.size() == 1){
                resultPath+=s2.pop();
            }else 
                resultPath+=(s2.pop()+'/');
        }
        if ( resultPath.equals("")){
            resultPath+="/";
        }
        return resultPath;
    }
   
}
// @lc code=end

